package com.itheima05;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 获取 class 对象
        Class<?> c = Class.forName("com.itheima02.Student");

        // 获取公共成员方法，包括继承的方法
        Method[] methods = c.getMethods();
        for (Method m : methods) {
            System.out.println(m);
        }
        System.out.println("------");

        // 获取全部成员方法
        Method[] declaredMethods = c.getDeclaredMethods();
        for (Method m : declaredMethods) {
            System.out.println(m);
        }
        System.out.println("------");

        // 获取单个方法
        Method method = c.getMethod("method1");

        // 获取无参构造方法创建对象
        Constructor<?> constructor = c.getConstructor();
        Object o = constructor.newInstance();

        method.invoke(o);

    }
}
